'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0121
    Inherits Snac

    Public Sub New()
        MyBase.New(&H1, &H21)
    End Sub

    Private _Notification As ExtendedStatusNotification
    Public ReadOnly Property Notification() As ExtendedStatusNotification
        Get
            Return _Notification
        End Get
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        If data.Count > index + 2 AndAlso (data(index) = 0 And data(index + 1) = 6) Then
            ' Icq sends information about the service version

            index += 2

            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            index += desc.TotalSize
        End If

        Dim type As ExtendedStatusNotificationType

        type = DirectCast(ByteConverter.ToUInt16(data.GetRange(index, 2)), ExtendedStatusNotificationType)

        Select Case type
            Case ExtendedStatusNotificationType.UploadIconRequest
                _Notification = New UploadIconNotification
            Case ExtendedStatusNotificationType.iChatAvialable
                _Notification = New iChatAvailableNotification
            Case Else
                Throw New NotImplementedException
        End Select

        _Notification.Deserialize(data.GetRange(index, data.Count - index))

        index += _Notification.TotalSize

        SetTotalSize(index)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function
End Class

Public MustInherit Class ExtendedStatusNotification
    Implements ISerializable

    Public Const SizeFixPart As Integer = 2

    Private _DataSize As Integer

    Public MustOverride Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return SizeFixPart + CalculateDataSize()
    End Function

    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize
        End Get
    End Property

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return SizeFixPart + DataSize
        End Get
    End Property

    Protected Sub SetDataSize(ByVal value As Integer)
        _DataSize = value
    End Sub

    Private _Type As ExtendedStatusNotificationType
    Public Property Type() As ExtendedStatusNotificationType
        Get
            Return _Type
        End Get
        Set(ByVal value As ExtendedStatusNotificationType)
            _Type = value
        End Set
    End Property

    Private _HasData As Boolean
    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Public Overridable Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        _Type = DirectCast(ByteConverter.ToUInt16(data.GetRange(0, 2)), ExtendedStatusNotificationType)

        _HasData = True
    End Sub

    Public Overridable Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Dim data As List(Of Byte)

        data = New List(Of Byte)

        data.AddRange(ByteConverter.GetBytes(_Type))

        Return data
    End Function
End Class

Public Class UploadIconNotification
    Inherits ExtendedStatusNotification

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function


    Private _IconFlag As UploadIconFlag
    Public Property IconFlag() As UploadIconFlag
        Get
            Return _IconFlag
        End Get
        Set(ByVal value As UploadIconFlag)
            _IconFlag = value
        End Set
    End Property

    Private _IconHash As New List(Of Byte)(16)
    Public ReadOnly Property IconHash() As List(Of Byte)
        Get
            Return _IconHash
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = SizeFixPart

        Dim hashLenght As Byte

        _IconFlag = DirectCast(data(index), UploadIconFlag)
        index += 1
        hashLenght = data(index)
        index += 1

        _IconHash.AddRange(data.GetRange(index, hashLenght))
        index += hashLenght

        SetDataSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function
End Class

Public Enum UploadIconFlag As Byte
    FirstUpload = &H41
End Enum

Public Class iChatAvailableNotification
    Inherits ExtendedStatusNotification

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        MyBase.Deserialize(data)

        Dim index As Integer = SizeFixPart

        Dim message As String

        message = ByteConverter.ToStringFromUInt16Index(index, data)
        index += 2
        If Not String.IsNullOrEmpty(message) Then index += message.Length

        SetDataSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function
End Class

Public Enum ExtendedStatusNotificationType As UShort
    UploadIconRequest = 1
    iChatAvialable = 2
End Enum